package com.all.play.myadaptertoadapter.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.all.play.myadaptertoadapter.R;
import com.all.play.myadaptertoadapter.bean.MyBean;
import com.all.play.myadaptertoadapter.fakefata.ResaultData;
import com.all.play.myadaptertoadapter.secondadapter.MySecondAdapter;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by 聪明一只哈 on 2018/5/18.
 * Time: 2018/5/18  9:57
 */

public class SecondActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private MySecondAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        EventBus.getDefault().register(this);

        initView();

        initData();
    }

    private void initData() {

        String data = ResaultData.data;
        Gson gson = new Gson();
        MyBean myBean = gson.fromJson(data, MyBean.class);
        if(myBean!=null){
            MyBean.ResultBean result = myBean.getResult();
            adapter = new MySecondAdapter(this, result);

            recyclerView.setLayoutManager(new LinearLayoutManager(this));

            recyclerView.setAdapter(adapter);


        }


    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_second);

        String data = ResaultData.data;


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MyBean.ResultBean.ActInfoBean infoBean) {

        Toast.makeText(SecondActivity.this, "第一个条目:使用EventBus传过来的数据:"+infoBean.getIcon_url(), Toast.LENGTH_SHORT).show();

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBeanEvent(MyBean.ResultBean.BannerInfoBean infoBean) {

        Toast.makeText(SecondActivity.this, "第二个条目:使用EventBus传过来的数据:"+infoBean.getImage()+"~~"+infoBean.getOption(), Toast.LENGTH_SHORT).show();

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
